import { MD5 } from "crypto-js";

export function getMD5Hash(input: string) {
  const md5Hash = MD5(input);
  const md5HashUpperCase = md5Hash.toString().toUpperCase();
  return md5HashUpperCase;
}

export function newDate(date: string | number | Date) {
  const sys = uni.getSystemInfoSync();
  const platform = sys.platform;
  let d = new Date(date);
  if (platform === "ios" && typeof date === "string") {
    const [year, month, day, hour, minute, second] = date.split(/[- : \/]/).map(Number);
    d = new Date(year, month - 1, day, hour, minute, second);
  }
  return d;
}

export function getDateTxt(date: string) {
  const d = newDate(date);
  const year = d.getFullYear();
  const month = d.getMonth() + 1;
  const day = d.getDate();
  const hour = d.getHours();
  const minute = d.getMinutes();
  const second = d.getSeconds();
  return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
}

/**
 * 判断是否过期
 */
export const isOverdue = (store: any) => {
  const expirationTime = store.getters?.userInfo?.vip_expiration;
  if (!expirationTime) return true;
  const expTime = new Date(expirationTime).getTime();
  const nowTime = new Date().getTime();
  // console.log('expirationTime', expirationTime, expTime, nowTime);
  return nowTime > expTime;
};

export const isOverdueInTime = (expirationTime: string) => {
  if (!expirationTime) return true;
  const expTime = new Date(expirationTime).getTime();
  const nowTime = new Date().getTime();
  // console.log('expirationTime', expirationTime, expTime, nowTime);
  return nowTime > expTime;
};

/**
 * 判断是否在微信中
 *
 */
export const isOnWechat = () => {
  let boolean = false;
  // #ifdef H5
  let ua = window.navigator.userAgent.toLowerCase();
  if (ua.match(/micromessenger/i) == "micromessenger") {
    //console.log('是微信客户端')
    boolean = true;
  } else {
    //console.log('不是微信客户端')
    boolean = false;
  }
  // #endif
  return boolean;
};
